package com.example.home.government.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.government.acivity.AppealMainActivity;
import com.example.home.government.acivity.AppealOtherMainActivity;
import com.example.home.government.bean.PersonAppealBean;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class PersonAppealAdapter extends RecyclerView.Adapter<PersonAppealAdapter.MyHoldy> {
    private Context context;
    private List<PersonAppealBean.RowsBean> list;

    public PersonAppealAdapter(Context context, List<PersonAppealBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_sever, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(250, 200);
        view.setLayoutParams(layoutParams);
        return new MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        Glide.with(context).load(ip + list.get(position).getImgUrl()).into(holder.sever_tv_img);
        holder.sever_iv_text.setText(list.get(position).getName());
           if (position != list.size()-1){
               holder.rootView.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent = new Intent(context,AppealMainActivity.class);
                       intent.putExtra("appeal_id",String.valueOf(list.get(position).getId()));
                       intent.putExtra("title",String.valueOf(list.get(position).getName()));
                       context.startActivity(intent);
                   }
               });
           }else if (list.get(position).getId() == 23){
               holder.rootView.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Intent intent = new Intent(context, AppealOtherMainActivity.class);
                       intent.putExtra("listIdae",String.valueOf(list.get(position).getId()));
                       context.startActivity(intent);
                   }
               });
           }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private final LinearLayout rootView;
        private final TextView sever_iv_text;
        private final ImageView sever_tv_img;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);
            sever_iv_text = itemView.findViewById(R.id.sever_iv_text);
            sever_tv_img = itemView.findViewById(R.id.sever_tv_img);
        }
    }
}
